package com.zl.autocode.service.autocode.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zl.autocode.common.constant.AutocodeEnum;
import com.zl.autocode.dao.autocode.ZlWebComponentMapper;
import com.zl.autocode.dto.autocode.webComponent.ReqWebComponentAddDTO;
import com.zl.autocode.dto.autocode.webComponent.ReqWebComponentDelDTO;
import com.zl.autocode.dto.autocode.webComponent.ReqWebComponentDetailDTO;
import com.zl.autocode.dto.autocode.webComponent.ReqWebComponentEditDTO;
import com.zl.autocode.dto.autocode.webComponent.ReqWebComponentQueryDTO;
import com.zl.autocode.dto.autocode.webComponent.ReqWebComponentStateDTO;
import com.zl.autocode.dto.autocode.webComponent.ResWebComponentAddDTO;
import com.zl.autocode.dto.autocode.webComponent.ResWebComponentDelDTO;
import com.zl.autocode.dto.autocode.webComponent.ResWebComponentDetailDTO;
import com.zl.autocode.dto.autocode.webComponent.ResWebComponentEditDTO;
import com.zl.autocode.dto.autocode.webComponent.ResWebComponentQueryDTO;
import com.zl.autocode.dto.autocode.webComponent.ResWebComponentStateDTO;
import com.zl.autocode.dto.autocode.webComponent.ResWebComponentVO2;
import com.zl.autocode.entity.autocode.ZlWebComponent;
import com.zl.autocode.entity.autocode.po.ZlWebComponentVo;
import com.zl.autocode.service.autocode.IWebComponentService;
import com.zl.autocode.service.base.AutocodeBaseManager;
import com.zl.common.dto.ReqDTO;
import com.zl.common.dto.pagination.PageReqVO;
import com.zl.common.enums.resp.GlobalRespEnum;
import com.zl.common.exception.BizException;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;

/**
 * 前端组件
 * @author ZL
 */
@Service("autocodeWebComponentService")
public class WebComponentServiceImpl extends AutocodeBaseManager implements IWebComponentService {

    @Autowired
    private ZlWebComponentMapper webComponentMapper;

    @Override
    public ResWebComponentQueryDTO queryWebComponentList(ReqDTO<ReqWebComponentQueryDTO> reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ReqWebComponentQueryDTO reqData = reqInfo.getReqData();
        PageReqVO page = pageHandle(reqInfo.getPageInfo());//处理分页数据
        ResWebComponentQueryDTO resDTO = BeanUtil.copyProperties(page, ResWebComponentQueryDTO.class);
        if(StringUtils.isNotBlank(reqData.getComponentTitle())){
            reqData.setComponentTitle("%"+reqData.getComponentTitle()+"%");
        }
        Map<String,Object> paramMap = BeanUtil.beanToMap(reqData,false,true);
        
        Page<Object> pageHp = PageHelper.startPage(page.getPage(), page.getLimit(),page.getSortorder());
        List<ZlWebComponentVo> list = webComponentMapper.selectListPage(paramMap);
        resDTO.setData(BeanUtil.copyToList(list, ResWebComponentVO2.class));
        resDTO.setCount(pageHp.getTotal());
        
        return resDTO;
    }
    
    @Override
    public ResWebComponentDetailDTO queryWebComponentDetail(ReqDTO<ReqWebComponentDetailDTO> reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ReqWebComponentDetailDTO reqData = reqInfo.getReqData();
        ResWebComponentDetailDTO res = new ResWebComponentDetailDTO();
        
        Map<String,Object> paramMap = new HashMap<>();
        paramMap = BeanUtil.beanToMap(reqData);
        ZlWebComponent view = webComponentMapper.selectOneByMap(paramMap);
        BeanUtil.copyProperties(view, res);
        return res;
    }
    
    @Override
    public ResWebComponentAddDTO insertWebComponentInfo(ReqDTO<ReqWebComponentAddDTO> reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ReqWebComponentAddDTO reqData = reqInfo.getReqData();
        ResWebComponentAddDTO res = new ResWebComponentAddDTO();
        Date time = getSystemTime().getDate();
        
        ZlWebComponent record = new ZlWebComponent();
        BeanUtil.copyProperties(reqData, record);
        
        initCreateInfo(record, reqInfo, time);
        record.setComponentId(IdUtil.simpleUUID());
        
        
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("componentId",record.getComponentId());
        //检查主键是否冲突
        if(webComponentMapper.countExistKey(paramMap)>0){
            throw new BizException(GlobalRespEnum.DUPLICATE_PRIMARY_KEY);
        }
        
        webComponentMapper.insertSelective(record);
        
        ZlWebComponent keyInfo = webComponentMapper.selectOneByMap(paramMap);
        BeanUtil.copyProperties(keyInfo, res);
        
        return res;
    }
    
    @Override
    public ResWebComponentEditDTO updateWebComponentInfo(ReqDTO<ReqWebComponentEditDTO> reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ReqWebComponentEditDTO reqData = reqInfo.getReqData();
        ResWebComponentEditDTO res = new ResWebComponentEditDTO();
        Date time = getSystemTime().getDate();
        
        ZlWebComponent record = new ZlWebComponent();
        BeanUtil.copyProperties(reqData, record);
        
        initUpdateInfo(record, reqInfo, time);
        
        webComponentMapper.updateByPrimaryKeySelective(record);
        
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("componentId",record.getComponentId());
        ZlWebComponent keyInfo = webComponentMapper.selectOneByMap(paramMap);
        BeanUtil.copyProperties(keyInfo, res);
        
        return res;
    }
    
    @Override
    public ResWebComponentDelDTO deleteWebComponentInfo(ReqDTO<ReqWebComponentDelDTO> reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ReqWebComponentDelDTO reqData = reqInfo.getReqData();
        ResWebComponentDelDTO res = new ResWebComponentDelDTO();
        Date time = getSystemTime().getDate();
        
        Map<String,Object> paramMap = BeanUtil.beanToMap(reqData);
        webComponentMapper.deleteByMap(paramMap);
        
        return res;
    }
    
    @Override
    public ResWebComponentStateDTO updateWebComponentState(ReqDTO<ReqWebComponentStateDTO> reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ReqWebComponentStateDTO reqData = reqInfo.getReqData();
        ResWebComponentStateDTO res = new ResWebComponentStateDTO();
        Date time = getSystemTime().getDate();
         
        ZlWebComponent record = new ZlWebComponent();
        BeanUtil.copyProperties(reqData, record);
        initUpdateInfo(record, reqInfo, time);
        webComponentMapper.updateByPrimaryKeySelective(record);
         
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("componentId",record.getComponentId());
        ZlWebComponent keyInfo = webComponentMapper.selectOneByMap(paramMap);
        BeanUtil.copyProperties(keyInfo, res);
        
        return res;
    }
}